package com.zw.yzk.kabu.mapper;

import com.zw.yzk.kabu.data.network.entity.GoodsOrderEntity;
import com.zw.yzk.kabu.data.network.task.GoodsOrderTask;
import com.zw.yzk.kabu.model.GoodsModel;
import com.zw.yzk.kabu.model.GoodsOrderModel;
import com.zw.yzk.kabu.utils.CalculateUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/3/13.
 */

public class GoodsOrderMapper {

    @Inject
    public GoodsOrderMapper() {
    }

    public GoodsOrderModel.Goods transfer(GoodsOrderEntity.Goods goods) {
        GoodsOrderModel.Goods model = new GoodsOrderModel.Goods();
        model.setId(goods.id);
        model.setOrderId(goods.orderId);
        model.setOriginalPrice(goods.originalPrice);
        model.setCount(goods.count);
        model.setProperty(goods.property);
        model.setTotalPrice(goods.totalPrice);
        model.setImage(goods.image);
        model.setDiscountedPrice(goods.discountedPrice);
        model.setGoodsId(goods.goodsId);
        model.setName(goods.name);
        model.setPrice(CalculateUtils.getRealPrice(goods.originalPrice, goods.discountedPrice));

        return model;
    }

    public List<GoodsOrderModel.Goods> transfer(List<GoodsOrderEntity.Goods> goodsList) {
        List<GoodsOrderModel.Goods> modelList = new ArrayList<>();
        if (goodsList == null || goodsList.isEmpty()) {
            return modelList;
        }
        for (GoodsOrderEntity.Goods goods : goodsList) {
            modelList.add(transfer(goods));
        }
        return modelList;
    }

    public GoodsOrderModel transfer(GoodsOrderEntity.Order order) {
        GoodsOrderModel model = new GoodsOrderModel();
        model.setTradeEndTime(order.tradeEndTime);
        model.setId(order.id);
        model.setReceiverAddress(order.receiverAddress);
        model.setTotalPrice(order.totalPrice);
        model.setReceiverName(order.receiverName);
        model.setUserId(order.userId);
        model.setUnifiedOrderId(order.unifiedOrderId);
        model.setFreight(order.freight);
        model.setReceiverInfoId(order.receiverInfoId);
        model.setCreateTime(order.createTime);
        model.setReceiverPhone(order.receiverPhone);
        model.setOrderId(order.orderId);
        model.setWxTransactionId(order.wxTransactionId);
        model.setShopId(order.shopId);
        model.setOrderType(order.orderType);
        model.setAirwayBill(order.airwayBill);
        model.setNote(order.note);
        model.setShopName(order.shopName);
        model.setLogisticId(order.logisticId);
        model.setGoodsList(transfer(order.goodsList));
        model.setRefundTime(order.refundTime);
        model.setRefundState(order.refundState);

        int count = 0;
        for (GoodsOrderModel.Goods goods : model.getGoodsList()) {
            count += goods.getCount();
        }
        model.setGoodsCount(count);

        switch (order.orderType) {
            case GoodsOrderTask.GOODS_ORDER_ALL:
                break;
            case GoodsOrderTask.GOODS_ORDER_COMPLETE:
                model.setOrderTypeStr("已完成");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNDELIVER:
                model.setOrderTypeStr("待发货");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNPAIED:
                model.setOrderTypeStr("待付款");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNRECEIVE:
                model.setOrderTypeStr("待收货");
                break;
            case GoodsOrderTask.GOODS_ORDER_BACK:
                model.setOrderTypeStr("退货");
                break;
            default:
                break;
        }
        return model;
    }

    public List<GoodsOrderModel> transfer(GoodsOrderEntity entity) {
        List<GoodsOrderModel> modelList = new ArrayList<>();
        if (entity.result.orderList == null || entity.result.orderList.isEmpty()) {
            return modelList;
        }
        for (GoodsOrderEntity.Order order : entity.result.orderList) {
            modelList.add(transfer(order));
        }
        return modelList;
    }
}
